package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.dao.StudentDao;
import com.example.data.request.ReqStudentList;
import com.example.data.response.RespUsernameAndPassword;
import com.example.data.response.ShowBaseStudentInfo;
import com.example.entity.Student;
import com.example.service.StudentService;
import com.example.utils.BCrypt;
import com.example.utils.JwtUtil;
import com.example.utils.R;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * Author:yanghua
 * Date:2023/7/20 18:25
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDao, Student> implements StudentService {
    @Resource
    private StudentDao studentDao;

    @Override
    public R addStudent(Student student) {
        String password = student.getPassword();
        student.setPassword(BCrypt.hashpw(password,BCrypt.gensalt()));
        int i = studentDao.insert(student);
        Map<String, Object> data = new HashMap<>();
        ShowBaseStudentInfo s = studentDao.selectStudentById(student.getId());
        data.put("info", s);
        if (i == 1) return R.ok(data,"添加成功");
        else return R.error("添加失败");
    }

    @Override
    public R getStudentList(ReqStudentList reqStudentList) {
        reqStudentList.setPage((reqStudentList.getPage() - 1) * reqStudentList.getSize());
        List<ShowBaseStudentInfo> studentList = studentDao.getStudentList(reqStudentList);
        Integer studentNum = studentDao.getStudentNum(reqStudentList);
        Map<String, Object> data = new HashMap<>();
        data.put("list", studentList);
        data.put("size", studentNum);
        return R.ok(data, "查询成功");
    }

    @Override
    public R updateByNumId(Student student) {
        String password = student.getPassword();
        student.setPassword(BCrypt.hashpw(password,BCrypt.gensalt())); // 加密
        int i = studentDao.updateById(student);
        ShowBaseStudentInfo s = studentDao.selectStudentById(student.getId());
        Map<String, Object> data = new HashMap<>();
        data.put("info", s);
        return i == 1 ? R.ok(data,"修改成功") : R.error("修改失败");
    }

    @Override
    public R deleteByNumId(String nid) {
        Integer i = studentDao.deleteByNumId(nid);
        return i == 1 ? R.ok("逻辑删除成功") : R.error("逻辑删除失败");
    }

    @Override
    public R getStudentUsernameAndPasswordByNid(String nid) {
        // 后期先判断是不是本人登录（要查的学号和登录的学号是否一致），管理员跳过
        RespUsernameAndPassword resp = studentDao.getStudentUsernameAndPasswordByNid(nid);
        Map<String, Object> data = new HashMap<>();
        data.put("data",resp);
        return R.ok(data,"查询成功");
    }

    @Override
    public R loginStudent(String username, String password) {
        // 1、访问数据库查询
        Student student = studentDao.getStudent(username);
        if (student == null) {
            return R.error("用户名不存在！");
        }
        if (!BCrypt.checkpw(password, student.getPassword())) {
            return R.error("密码错误！");
        }
        // 2、jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", student.getId());
        claims.put("username", student.getUsername());
        claims.put("numId", student.getNumId());
        String token = JwtUtil.getToken(claims);
        Map<String, Object> data = new HashMap<>();
        data.put("token", token);
        return R.ok(data, "登录成功");
    }

    @Override
    public R getStudentInfo(String token) {
        System.out.println(token);
        // 根据账号查人
        Map<String, Object> map = JwtUtil.parseToken(token);
        System.out.println(map);
        String username = (String) map.get("username");
        Student student = studentDao.getStudent(username);
        Map<String, Object> data = new HashMap<>();
        data.put("info", student);
        return R.ok(data, "查询成功");
    }
}
